/**
 * 
 */
package com.ctg.payroll2.bean;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import com.googlecode.jsonplugin.annotations.JSON;
import com.ctg.payroll2.bean.po.CsOrgchart;

/**
 * @author 郑致力
 *
 */
public class CsOrgchartTree {
	private Integer id;
	private String text;
	private boolean leaf;
	
	private List<CsOrgchartTree> children = new ArrayList<CsOrgchartTree>();
	
	public CsOrgchartTree() {
		
	}
	
	public CsOrgchartTree(CsOrgchart chart) {
		this.id = chart.getOrgchart_id();
		this.text = chart.getOrgchart_name();
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public boolean getLeaf() {
		return children.isEmpty();
	}

	public void setLeaf(boolean leaf) {
		this.leaf = leaf;
	}

	public List<CsOrgchartTree> getChildren() {
		return children;
	}

	public void setChildren(List<CsOrgchartTree> children) {
		this.children = children;
	}
	
	public void addChild(CsOrgchartTree t) {
		children.add(t);
	}
	
	public CsOrgchartTree findChindren(Integer id) {
//		System.out.println("Will find " + id + "i am is " + this.id);
		CsOrgchartTree _returnValue = null;
		if (this.id == null) {
//			System.out.println("this.id == null");
			return null;
		}
		if (this.id.intValue() == id.intValue()) {
//			System.out.println("this.id.intValue() == id.intValue()");
			return this;
		}
		Iterator<CsOrgchartTree> items = children.iterator();
		while (items.hasNext()) {
			CsOrgchartTree t = items.next();
			if (t.getId().intValue() == id.intValue()) {
//				System.out.println(t.getId() + " is " + id + "father." );
				return t;
			} else {
				_returnValue = t.findChindren(id);
				if (_returnValue != null) {
//					System.out.println(_returnValue.getId() + " is " + id + "father." );
					return _returnValue;
				}
			}
		}
//		System.out.println("return null");
		return null;
	}
}
